﻿//
// Copyright (c) 2024 BPergaofeng
//
#include <bperplayer/BPerPlayerDecoder.h>
#include <bperdecoder/BPerDecoderFactory.h>

BPerPlayerDecoder::BPerPlayerDecoder(BPerContext* pcontext) {
	m_context=pcontext;
	m_out_audioBuffer=NULL;
	m_out_videoBuffer=NULL;
	m_audioDec=NULL;
	m_videoDec=NULL;

}

BPerPlayerDecoder::~BPerPlayerDecoder() {
	if(m_audioDec&&m_audioDec->m_isStart){
		m_audioDec->stop();
		while(m_audioDec->m_isStart){
								bPer_usleep(1000);
							}
	}
	if(m_videoDec&&m_videoDec->m_isStart){
		m_videoDec->stop();
		while(m_videoDec->m_isStart){
								bPer_usleep(1000);
							}
	}
	//bPer_usleep(50000);
	bPer_delete(m_audioDec);
	bPer_delete(m_videoDec);
	//int32_t i=0;
	if(m_out_videoBuffer){
		delete m_out_videoBuffer;m_out_videoBuffer=NULL;
	}
	if(m_out_audioBuffer){
		delete m_out_audioBuffer;m_out_audioBuffer=NULL;
	}

}
void BPerPlayerDecoder::stopAll(){
	if(m_audioDec)	m_audioDec->stop();
	if(m_videoDec)	m_videoDec->stop();
}


void BPerPlayerDecoder::initAudioDecoder(){
	if(m_out_audioBuffer==NULL) {
		m_out_audioBuffer=new BPerAudioPlayBuffer();

	}
	if(m_audioDec==NULL) {
	//	BPerDecoderFactory df;
		//BPerAudioParam audio={0};

		m_audioDec=new BPerAudioDecoderHandle(m_context);
		m_audioDec->init();
		m_audioDec->setOutAudioBuffer(m_out_audioBuffer);
	}
}

void BPerPlayerDecoder::initVideoDecoder(){
	//BPerConfig *p_config = m_context;
	if(m_out_videoBuffer==NULL) {
		m_out_videoBuffer=new BPerVideoBuffer(1);
		//m_out_videoBuffer->m_syn=&m_syn;
	}
	//m_context->videoBuffers=m_out_videoBuffer;
//	BPerDecoderFactory df;
	if(m_videoDec==NULL) {

		m_videoDec=new BPerVideoDecoderHandle(m_context);
		m_videoDec->init();
		m_videoDec->setOutVideoBuffer(m_out_videoBuffer);
	}

}

void BPerPlayerDecoder::startAudioDecoder(){
	if(m_audioDec&&!m_audioDec->m_isStart)		if(m_audioDec) m_audioDec->start();
}

void BPerPlayerDecoder::startVideoDecoder(){
		if(m_videoDec&&!m_videoDec->m_isStart) m_videoDec->start();
}

void BPerPlayerDecoder::setInVideoBuffer(BPerVideoDecoderBuffer *pvel){
	if(m_videoDec!=NULL) m_videoDec->setInVideoBuffer(pvel);
}
void BPerPlayerDecoder::setInAudioBuffer(BPerAudioEncoderBuffer *pael){
	if(m_audioDec!=NULL) m_audioDec->setInAudioBuffer(pael);
}
BPerVideoBuffer* BPerPlayerDecoder::getOutVideoBuffer(){
	return m_out_videoBuffer;
}
BPerAudioPlayBuffer*  BPerPlayerDecoder::getOutAudioBuffer(){
	return  m_out_audioBuffer;
}

